package com.haixiaoke.saas.material.service.impl;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.util.StrUtil;
import com.haixiaoke.saas.common.config.OssConfig;
import com.haixiaoke.saas.common.oss.AliOssServiceImpl;
import com.haixiaoke.saas.common.tenant.TenantContext;
import com.haixiaoke.saas.common.utils.DateUtils;
import com.haixiaoke.saas.material.domain.MaterialGroup;
import com.haixiaoke.saas.material.domain.MaterialPhoto;
import com.haixiaoke.saas.material.mapper.MaterialPhotoMapper;
import com.haixiaoke.saas.material.service.IMaterialPhotoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import static com.haixiaoke.saas.system.framework.security.util.SecurityUtils.getUsername;

/**
 * 图片素材Service业务层处理
 * 
 * @author panda
 * @date 2023-12-25
 */
@Service
public class MaterialPhotoServiceImpl implements IMaterialPhotoService {
    @Autowired
    private MaterialPhotoMapper materialPhotoMapper;
    @Autowired
    private AliOssServiceImpl aliOssService;
    @Autowired
    private OssConfig ossConfig;

    /**
     * 查询图片素材
     * 
     * @param id 图片素材主键
     * @return 图片素材
     */
    @Override
    public MaterialPhoto selectMaterialPhotoById(Long id) {
        return materialPhotoMapper.selectMaterialPhotoById(id);
    }

    /**
     * 查询图片素材列表
     * 
     * @param materialPhoto 图片素材
     * @return 图片素材
     */
    @Override
    public List<MaterialPhoto> selectMaterialPhotoList(MaterialPhoto materialPhoto) {
        return materialPhotoMapper.selectMaterialPhotoList(materialPhoto);
    }

    /**
     * 新增图片素材
     * 
     * @param materialPhoto 图片素材
     * @return 结果
     */
    @Override
    public int insertMaterialPhoto(Long groupId,List<MultipartFile> files) {
        List<MaterialPhoto> materialPhotos = new ArrayList<>();
        for (MultipartFile file : files) {
            // 上传并返回新文件名称
            String photoUrl = aliOssService.upload(ossConfig.getMaterialPhotoName(), file.getOriginalFilename(), file);
            if (StrUtil.isNotBlank(photoUrl)) {
                MaterialPhoto insertPhoto = new MaterialPhoto();
                insertPhoto.setTenantId(TenantContext.getTenantId());
                insertPhoto.setCreateBy(getUsername());
                insertPhoto.setGroupId(groupId);
                insertPhoto.setPhotoUrl(photoUrl);
                insertPhoto.setCreateTime(DateUtils.getNowDate());
                materialPhotos.add(insertPhoto);
            }
        }
        return materialPhotoMapper.insertBatchMaterialPhoto(materialPhotos);
    }

    /**
     * 修改图片素材
     * 
     * @param materialPhoto 图片素材
     * @return 结果
     */
    @Override
    public int updateMaterialPhoto(MaterialPhoto materialPhoto) {
        materialPhoto.setUpdateTime(DateUtils.getNowDate());
        return materialPhotoMapper.updateMaterialPhoto(materialPhoto);
    }

    /**
     * 批量删除图片素材
     * 
     * @param ids 需要删除的图片素材主键
     * @return 结果
     */
    @Override
    public int deleteMaterialPhotoByIds(Long[] ids) {
        return materialPhotoMapper.deleteMaterialPhotoByIds(ids);
    }

    /**
     * 删除图片素材信息
     * 
     * @param id 图片素材主键
     * @return 结果
     */
    @Override
    public int deleteMaterialPhotoById(Long id) {
        return materialPhotoMapper.deleteMaterialPhotoById(id);
    }

    /**
     * 软删除图片素材
     * @param ids 图片素材id集合
     * @return
     */
    @Override
    public int softDeleteMaterialPhotoByIds(MaterialPhoto materialPhoto) {
        return materialPhotoMapper.softDeleteMaterialPhotoByIds(materialPhoto);
    }

    @Override
    public int softDeletePhotoByGroupIds(MaterialGroup group) {
        return materialPhotoMapper.softDeletePhotoByGroupIds(group);
    }
}
